package com.example.http.chapter5;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 了解HTTP关于代理服务器
 * 这里已nginx做代理服务器进行测试
 *
 * @author zhouxl
 * @DATE 2021/1/21
 * @descriptoin
 */
@Controller
@RequestMapping("api/proxy")
public class ProxySeverNginx {

    /**
     * nginx代理服务器页面
     *
     * @return java.lang.String
     * @author zhouxl
     * @date 2021-01-21
     */
    @RequestMapping("proxyNginxPage")
    public String proxyNginxPage1() {
//        R( /api/proxy/proxyNginxPage)
        return "nginx";
    }


    /**
     * 测试json数据格式
     *
     * @return java.lang.String
     * @author zhouxl
     * @date 2021-01-21
     */
    @RequestMapping("proxyNginxJson")
    @ResponseBody
    public String proxyNginxJson() {
        return "proxyNginxJson";
    }

    /**
     * 测试文件下载处理
     *
     * @param response
     * @return java.lang.String
     * @author zhouxl
     * @date 2021-01-21
     */
    @RequestMapping("proxyNginxFileDown")
    @ResponseBody
    public String proxyNginxFileDown(HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        FileInputStream fis = new FileInputStream("D:\\nginx-1.16.1\\logs\\error.log");
        IOUtils.copy(fis, response.getOutputStream());
        return null;
    }

}
